/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jjsoft.disco.gco.view;

import com.jjsoft.disco.gco.mdl.Producto;
import com.jjsoft.disco.gco.ngc.ProductoBarraManager;
import com.jjsoft.disco.gco.ngc.ProductoManager;
import com.jjsoft.disco.gco.util.CImprimir;
import com.jjsoft.disco.gco.util.CNumeros;
import com.jjsoft.disco.gco.util.MiModelo;
import com.jjsoft.disco.gco.util.configuraciones;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author Johnny
 */
public class VReporteStock extends javax.swing.JFrame {

    /**
     * Creates new form VReporteStock
     */
    ProductoManager productoManager = new ProductoManager();
    List<Producto> listaProductos;
   // Producto producto = new Producto();
    public VReporteStock() {
        initComponents();
        cboBarra.addItem("Bodega");
        for(int i=0; i<configuraciones.barras;i++)
        {
            cboBarra.addItem("Barra "+(i+1));
        }
        
        listaProductos=productoManager.listarStockCompleto();
        SpinnerModel model = new SpinnerNumberModel(-1, -1, 10, 1);
        txtStockMinimo.setModel(model);
        cargarDatos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        dtgProductos = new javax.swing.JTable();
        lblLocal = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cboBarra = new javax.swing.JComboBox();
        txtStockMinimo = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        cmdImprimir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        dtgProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        dtgProductos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(dtgProductos);

        lblLocal.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        lblLocal.setForeground(new java.awt.Color(0, 0, 255));
        lblLocal.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblLocal.setText("Reporte de Stock");

        jButton1.setText("Salir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Stock de :");

        cboBarra.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todo" }));
        cboBarra.setToolTipText("");
        cboBarra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboBarraActionPerformed(evt);
            }
        });

        txtStockMinimo.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                txtStockMinimoStateChanged(evt);
            }
        });

        jLabel2.setText("Stock Minimo:");

        cmdImprimir.setText("Imprimir");
        cmdImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdImprimirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                    .addComponent(lblLocal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(4, 4, 4)
                                .addComponent(cboBarra, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtStockMinimo, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmdImprimir, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblLocal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboBarra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtStockMinimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(cmdImprimir))
                .addContainerGap())
        );

        setSize(new java.awt.Dimension(575, 380));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void cboBarraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboBarraActionPerformed
        // TODO add your handling code here:
        if (evt.getModifiers()==16)
        {
            if(cboBarra.getSelectedIndex()==0)
            {
                listaProductos=productoManager.listarStockCompleto(Integer.parseInt(String.valueOf(txtStockMinimo.getValue())));
            }
            else if(cboBarra.getSelectedIndex()==1)
            {
                listaProductos=productoManager.listarProductos(0, Integer.parseInt(String.valueOf(txtStockMinimo.getValue())));
                
            }
            else
            {
                int barra=cboBarra.getSelectedIndex()-1;
                listaProductos= productoManager.buscarPorBarra(barra, Integer.parseInt(String.valueOf(txtStockMinimo.getValue())));
            }
            cargarDatos();
        }
    }//GEN-LAST:event_cboBarraActionPerformed

    private void txtStockMinimoStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_txtStockMinimoStateChanged
        // TODO add your handling code here:
         if(cboBarra.getSelectedIndex()==0)
            {
                listaProductos=productoManager.listarStockCompleto(Integer.parseInt(String.valueOf(txtStockMinimo.getValue())));
            }
            else if(cboBarra.getSelectedIndex()==1)
            {
                listaProductos=productoManager.listarProductos(0, Integer.parseInt(String.valueOf(txtStockMinimo.getValue())));
                
            }
            else
            {
                int barra=cboBarra.getSelectedIndex()-1;
                listaProductos= productoManager.buscarPorBarra(barra, Integer.parseInt(String.valueOf(txtStockMinimo.getValue())));
            }
            cargarDatos();
    }//GEN-LAST:event_txtStockMinimoStateChanged

    private void cmdImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdImprimirActionPerformed
        // TODO add your handling code here:
        if(JOptionPane.showConfirmDialog(rootPane,"Esta seguro?","Imprimir Stock",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){
            new CImprimir().imprimirStock(listaProductos);
        }
    }//GEN-LAST:event_cmdImprimirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VReporteStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VReporteStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VReporteStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VReporteStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VReporteStock().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cboBarra;
    private javax.swing.JButton cmdImprimir;
    private javax.swing.JTable dtgProductos;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblLocal;
    private javax.swing.JSpinner txtStockMinimo;
    // End of variables declaration//GEN-END:variables

    private void cargarDatos() {
        MiModelo modelo = new MiModelo();
        String[] identificadores = new String[]{"Cod", "Descripcion","Categoria", "P. Venta", "Stock"};
        modelo.setColumnIdentifiers(identificadores);
        for (int i = 0; i < listaProductos.size(); i++) {
                String[] producto = new String[]{listaProductos.get(i).getCodigo(), listaProductos.get(i).getDescripcion(), listaProductos.get(i).getIdCategoria().getNombre(), CNumeros.Formato(listaProductos.get(i).getPrecioVenta()), CNumeros.Formato(listaProductos.get(i).getStock())};
                modelo.addRow(producto);
            
        }
        dtgProductos.setModel(modelo);
        dtgProductos.getColumn("Cod").setPreferredWidth(45);
        dtgProductos.getColumn("Descripcion").setPreferredWidth(250);
        dtgProductos.getColumn("Categoria").setPreferredWidth(50);
    }
}
